
AIDS.Emotes = {}

if CLIENT then

	local function ChatEmote( ply, text, isteam, isdead )
	
		for k,v in pairs( AIDS.Emotes ) do
		
			if string.lower( text ) == string.lower( v[1] ) then
				
				if ply == LocalPlayer() then
			
					RunConsoleCommand( "aids_play_emote", v[2] )
					
				end
				
				return true
			
			end
		
		end
	
	end
	AIDS.Hook( "OnPlayerChat", "emotes", ChatEmote )
	
	local function EmoteSynch( handler, id, encoded, decoded )
	
		AIDS.Emotes = decoded
	
	end
	datastream.Hook( "EmoteSynch", EmoteSynch )
	
	local function RemoveEmote( id )
	
		RunConsoleCommand( "aids_remove_emote", id )
	
	end
	
	local function AddEmote( pattern, sound )
	
		RunConsoleCommand( "aids_add_emote", pattern, sound )
	
	end
	
	local function MakePanel()

		local panel = vgui.Create( "DPanel" )
		panel.InvalidateLayout = function()
		
			if panel.List then
			
				panel.List:StretchToParent( 0, 35, 0, 0 )
				
				panel.Box1:SetPos( 0, 7 )
				panel.Box2:SetPos( 165, 7 )
				panel.Butan:SetPos( 330, 5 )
				
			end
		
		end
		
		local textbox1 = vgui.Create( "DTextEntry", panel )
		textbox1:SetWide( 150 )
		textbox1:SetText( "Pattern" )
		panel.Box1 = textbox1
		
		local textbox2 = vgui.Create( "DTextEntry", panel )
		textbox2:SetWide( 150 )
		textbox2:SetText( "Sound.wav" )
		panel.Box2 = textbox2
		
		local butan = vgui.Create( "DButton", panel )
		butan:SetText( "Add Emote" )
		butan:SetWide( 100 )
		butan.DoClick = function()
		
			butan.List:AddLine( textbox1:GetText(), textbox2:GetText() )
			AddEmote( textbox1:GetText(), textbox2:GetText() )
			surface.PlaySound( Sound( "buttons/button14.wav" ) )
		
		end
		panel.Butan = butan
		
		local list = vgui.Create( "DListView", panel )
		panel.List = list
		butan.List = list
		
		local col1 = list:AddColumn( "Pattern" )
		local col2 = list:AddColumn( "Sound" )
		col1:SetMinWidth( 200 )
		col1:SetMaxWidth( 200 )
		
		return panel
		
	end

	local function PopulateList( panel )
	
		local list = panel.List
		
		if not list then return end
	
		for k,v in pairs( AIDS.Emotes ) do
		
			list:AddLine( v[1], v[2] )
		
		end
	
		list.OnRowRightClick = function( line, id )
	
			local menu = vgui.Create( "DMenu", list )
			menu:AddOption( "Cancel" )
			menu:AddOption( "Delete", function() 
				
				list:RemoveLine( id ) 
				table.remove( AIDS.Emotes, id ) 
				
				RemoveEmote( id )
				
				surface.PlaySound( Sound( "buttons/button14.wav" ) )
				
			end )
	
		end
		
	end

	AIDS.AddMenuTab( "Emotes", "gui/silkicons/sound", "Manage the chat emotes used by the server.", MakePanel, PopulateList )

else

	local function EmoteInitPly( ply )
	
		datastream.StreamToClients( { ply }, "EmoteSynch", AIDS.Emotes )
	
	end
	
	AIDS.Hook( "PlayerInitialSpawn", "emote", EmoteInitPly )
	
	local function EmoteInit()
	
		local str = file.Read( "AIDS/chat_emotes.txt" )
		local tbl = string.Explode( "\n", ( str or "" ) )
		
		for k,v in pairs( tbl ) do
		
			table.insert( AIDS.Emotes, string.Explode( "@", v ) )
		
		end
	
	end
	
	AIDS.Hook( "Initialize", "emote", EmoteInit )

	local function WriteToFile()
	
		local str = ""
	
		for k,v in pairs( AIDS.Emotes ) do
		
			str = str .. "\n" .. v[1] .. "@" .. v[2]
		
		end
		
		str = string.Right( str, string.len( str ) - 1 )
		
		file.Write( "AIDS/chat_emotes.txt", str )
		
		print( "[AIDS] Updated chat emotes file." )
	
	end
	
	local function Synch()
	
		datastream.StreamToClients( player.GetAll(), "EmoteSynch", AIDS.Emotes )
	
	end
	
	local function RemoveEmote( ply, cmd, args )
	
		if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) or not args[1] then return end
		
		table.remove( AIDS.Emotes, tonumber( args[1] ) )
		
		WriteToFile()
		Synch()
	
	end
	concommand.Add( "aids_remove_emote", RemoveEmote )

	local function AddEmote( ply, cmd, args )
	
		if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) or not args[1] or not args[2] then return end
		
		table.insert( AIDS.Emotes, { args[1], args[2] } )
		
		WriteToFile()
		Synch()
	
	end
	concommand.Add( "aids_add_emote", AddEmote )
	
	local function PlayEmote( ply, cmd, args )
	
		if not args[1] then return end
		
		for k,v in pairs( AIDS.Emotes ) do
		
			if v[2] == args[1] then
			
				if ( ply.EmoteTimer or 0 ) < CurTime() then
		
					ply:EmitSound( args[1] )
					ply.EmoteTimer = CurTime() + 0.5
					
					return
		
				end
			
			end
		
		end
	
	end
	concommand.Add( "aids_play_emote", PlayEmote )
	
end